package com.xm.xmapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.adapter.QuestionListAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_question_listRequest;
import com.xm.xmapp.response.bean.BaseListResponse;
import com.xm.xmapp.response.bean.ResponseQuestionListBean;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.MyCache;

public class QuestionListActivity extends BaseActivity {

	private XListView xListView;
	private QuestionListAdapter adapter;
	private ArrayList<ResponseQuestionListBean> list;
	private RelativeLayout rel;
	private Get_question_listRequest requestBean;
	private GetDataInterFace<String> listener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question_list);
	}

	@Override
	public void initView() {

		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {

		setMyTitle("常见问题");
		list = new ArrayList<ResponseQuestionListBean>();
		adapter = new QuestionListAdapter(list, this, R.layout.item_question_list);
		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(false);
		requestBean = new Get_question_listRequest();
		requestBean.type = 0;

		listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				xListView.stopRefresh();
				xListView.stopLoadMore();
				BaseListResponse<ResponseQuestionListBean> bean = JSON.parseObject(response,
						new TypeReference<BaseListResponse<ResponseQuestionListBean>>() {
						});
				Helper_SharedPreferences.set_int_sp(CodeConstant.Question_version_number, bean.question_version_number);
				if (bean.list == null || bean.list.size() == 0) {
					rel.setVisibility(View.VISIBLE);
					xListView.setVisibility(View.GONE);
				} else {
					list.clear();
					rel.setVisibility(View.GONE);
					xListView.setVisibility(View.VISIBLE);
				}
				list.addAll(bean.list);
				if (list.size() == 0) {
					Helper_SharedPreferences.set_int_sp(CodeConstant.Question_version_number, 0);
				}
				adapter.notifyDataSetChanged();
			}
		};
		if (!getMyCache())
			get_question_listRequest();
	}

	private boolean getMyCache() {

		String response = MyCache.getMyCache().getCache(requestBean.getCacheString());
		if (response != null && !response.equals("")) {
			listener.onResponse(response);
			return true;
		}
		Helper_SharedPreferences.set_int_sp(CodeConstant.Question_version_number, 0);
		return false;
	}

	private void get_question_listRequest() {

		requestBean.question_version_number = Helper_SharedPreferences.get_int_sp(CodeConstant.Question_version_number);
		MyAppcation.getMyAppcation().getPostData(this, requestBean, listener, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});
	}

	@Override
	public void initEvent() {
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				requestBean.type = 1;
				get_question_listRequest();
			}

			@Override
			public void onLoadMore() {
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});
	}


}
